package com.fosdata.dpm.util;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Description: math相关工具类
 * @Author cy
 * @Date 2022/7/12 12:14
 */
public class MathUtils {

    /**
     * 保留小数位位数
     **/
    public static BigDecimal setScale(BigDecimal value, Integer digit) {
        if (null == value) {
            return null;
        }
        if (null == digit) {
            digit = 2;
        }
        return value.setScale(digit, RoundingMode.HALF_UP);

    }

    /**
     * 保留小数位位数
     **/
    public static BigDecimal setScale(String value, Integer digit) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        if (null == digit) {
            digit = 2;
        }
        return new BigDecimal(value).setScale(digit, RoundingMode.HALF_UP);

    }


    /**
     * 保留小数位位数
     **/
    public static Double setScale(Double value, Integer digit) {
        if (null==value) {
            return null;
        }
        if (null == digit) {
            digit = 2;
        }
        return new BigDecimal(value).setScale(digit, RoundingMode.HALF_UP).doubleValue();

    }

    /**
     * 保留小数位位数
     **/
    public static String setScale(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        return new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).toString();

    }


    public static Double setScaled(Object value) {
        if (null == value) {
            return null;
        }
        return new BigDecimal(value.toString()).setScale(2, RoundingMode.HALF_UP).doubleValue();

    }

    /**
     * 保留小数位位数
     **/
    public static String setScaleWithZero(String value, Integer digit) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        if (null == digit) {
            digit = 2;
        }
        if (digit == 0) {
            //不带小数位
            return String.valueOf(Integer.parseInt(value));
        }
        return new BigDecimal(value).setScale(digit, RoundingMode.HALF_UP).toString();

    }


    /**
     * 分钟转天数
     **/
    public static Double minuteToDay(Object minutes) {
        if (null == minutes) {
            return null;
        }
        return new BigDecimal(minutes.toString()).divide(new BigDecimal(1440), 2, RoundingMode.HALF_UP).doubleValue();
    }


    public static String minuteToHours(String minutes) {
        if (null == minutes) {
            return null;
        }
        return new BigDecimal(minutes).divide(new BigDecimal(60), 4, RoundingMode.HALF_UP).toString();
    }

    public static String hourToDay(String hour,Integer scale){
        if(StringUtils.isEmpty(hour)){
            return null;
        }
        return new BigDecimal(hour).divide(new BigDecimal(24),scale,BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String secondToMinutes(Long seconds,Integer scale) {
        if (null == seconds) {
            return "0.0";
        }
        return new BigDecimal(seconds).divide(new BigDecimal(60), scale, RoundingMode.HALF_UP).toString();
    }

    /**
     * 保留一位小数
     */
    public static Double round1(Double d) {
        BigDecimal bg = new BigDecimal(d);
        return bg.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    }


    public static String round1Str(Double b) {
        BigDecimal db = new BigDecimal(b.toString());
        return String.format("%.1f", db);
    }


    public static String round2Str(Double b) {
        BigDecimal db = new BigDecimal(b.toString());
        return String.format("%.2f", db);
    }


    /**
     * 保留两位小数
     */
    public static Double round2(Double d) {
        return Double.valueOf(String.format("%.2f", d));
    }

    /**
     * 小数位格式化处理
     **/
    public static String formatValue(String value, Integer numOfBits) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        if (null == numOfBits) {
            return value;
        }
        if (numOfBits == 0) {
            value = String.valueOf(new BigDecimal(value).intValue());
        } else {
            BigDecimal bigDecimal = MathUtils.setScale(value, numOfBits);
            if (null != bigDecimal) {
                value = bigDecimal.toString();
            }
        }
        return value;
    }

    public static Boolean judjeNaN(String value) {
        if (StringUtils.isEmpty(value)) {
            return null;
        }
        if (Double.isNaN(Double.parseDouble(value))) {
            return true;
        }
        return false;

    }

    public static Boolean isZero(String value) {
        if (StringUtils.isEmpty(value)) {
            return true;
        }
        if (new BigDecimal(value).compareTo(new BigDecimal("0")) == 0) {
            return true;
        }
        return false;

    }

    public static Boolean isZero(BigDecimal value){
        if(null==value){
            return true;
        }
        if(value.compareTo(new BigDecimal("0"))==0){
            return true;
        }
        return false;

    }

    public static boolean isNegative(String value) {
        if(StringUtils.isEmpty(value)){
            return false;
        }
        if(new BigDecimal(value).compareTo(new BigDecimal(0))<0){
            return true;
        }
        return false;
    }
}
